// app.js
App({
  onLaunch() {
    // 展示本地存储能力
    let sysInfo = wx.getStorageSync('systemInfo');
    if (!sysInfo) {
      sysInfo = wx.getWindowInfo();
      wx.setStorage({
        key: 'systemInfo',
        data: sysInfo
      });
    }
    this.globalData.safearea = sysInfo.safeArea;
    this.globalData.sysInfo = sysInfo;
    this.globalData.category = wx.getStorageSync('category');

    wx.cloud.init({env: 'exam-4gpi8ydje868ff67'});
    const that = this
    wx.cloud.callFunction({
      name:'getOpenid'
    }).then(res => {
      console.log(res)
      // res.result.openid = 'o4rcL7dTByplNs2oA8KnNFq3WLx4' // 测试代码
      that.globalData.openid = res.result.openid;
      that.request('/api/user/wxlogin', {openid: res.result.openid}, 'POST').then(resv => {
        if (resv.data.code === 1) {
          that.globalData.token = resv.data.data.userinfo.token;
          delete resv.data.data.userinfo.token;
          that.globalData.userinfo = resv.data.data.userinfo;
          let rdata = {};
          if (that.globalData.category) {
            rdata.question_category_id = that.globalData.category.id;
          }
          that.request('/api/user/info', rdata).then(res => {
            if (res.data.code === 1) {
              that.globalData.userinfo = res.data.data;
            }
            that.globalData.isLogin = true;
            if (that.callback) {
              that.callback(true);
            }
          }).catch(err => {
            console.error(err);
          })
        } else {
          that.globalData.isLogin = true;
          if (that.callback) {
            that.callback(false);
          }
        }
      }).catch(err => {
        console.error(err);
      })
    }).catch(err => {
      console.log(err)
    });
  },
  register(e) {
    let that = this;
    return new Promise((resolve, reject) => {
      if (!e.detail.cloudID) {
        wx.showToast({
          title: '注册失败',
          icon: 'none'
        });
        reject('注册失败');
      } else {
        wx.cloud.callFunction({ //连接云函数
          name:'getPhoneNumber', // 云函数名称
          data:{
            weRunData: wx.cloud.CloudID(e.detail.cloudID) // 这个 CloudID 值到云函数端会被替换
          }
        }).then(res => {
          console.log('手机号获取结果', res)
          that.globalData.moblie = res.result.moblie;
          let reqData = {openid: res.result.openid, mobile: res.result.moblie};
          if (that.globalData.invite_user_id) {
            reqData.invite_user_id = that.globalData.invite_user_id;
          }
          that.request('/api/user/wxlogin', reqData, 'POST').then(resv => {
            if (resv.data.data) {
              that.globalData.token = resv.data.data.userinfo.token;
              delete resv.data.data.userinfo.token;
              that.globalData.userinfo = resv.data.data.userinfo;
            }
            resolve(resv);
          }).catch(err => {
            reject(err);
          });
        }).catch(err=>{
          console.log('注册失败', err);
          reject(err);
        })
      }
    });
  },
  request(path, data={}, method='GET') {
    return new Promise((resolve, reject) => {
      wx.request({
        url: this.globalData.requestUrl + path,
        header: {
          'TOKEN': this.globalData.token
        },
        method: method,
        data: data,
        success: function (res) {
          resolve(res);
        },
        fail: function (err) {
          reject(err);
        }
      })
    })
  },
  globalData: {
    exam1ParamsNeedLoad: true,
    exam1Title: '模拟考试',
    exam1ViewMode: false,
    exam1ShowAnswer: false,
    exam1Params: {},
    token: '',
    isLogin: false,
    requestUrl: 'https://www.jtibao.cn',

    phoneNumber: [
      '17157465175',
      '15316987326'
    ]
  }
})
